package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.configuration.ConfCiRelationTypeProperty;
import itsm.isperp.module.service.configuration.ConfCiRelationTypePropertyService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-06-12 16:17
 */
@Controller
@RequestMapping("configuration/ciRelationTypeProperty")
public class ConfCiRelationTypePropertyController extends
		BaseController<ConfCiRelationTypeProperty> {

	@Autowired
	protected ConfCiRelationTypePropertyService confCiRelationTypePropertyService;

	@Override
	public ConfCiRelationTypePropertyService getService() {
		return this.confCiRelationTypePropertyService;
	}

	@Override
	public String getPageTitle() {
		return "配置关系属性";
	}

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {

		String relationTypeId = req.getParameter("relationTypeId");

		ModelAndView mv = this.getEdit(id);

		if (StringUtils.isEmpty(id)) {
			((ConfCiRelationTypeProperty) mv.getModel().get("model"))
					.setRelationTypeId(relationTypeId);
		}

		return mv;
	}

	@RequestMapping("listByRelationType")
	@ResponseBody
	public List<ConfCiRelationTypeProperty> listByRelationType(
			String relationTypeId, HttpServletRequest req) {
		JqGridRequest request = new JqGridRequest();
		request.addFilterRule("relationTypeId", JqGridFilterRuleOp.eq,
				relationTypeId);
		request.setSort("orderNum", Direction.ASC);
		return this.getService().findAll(request);

	}

	@RequestMapping("listDetail")
	@ResponseBody
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		String relationTypeId = req.getParameter("relationTypeId");

		request.addFilterRule("relationTypeId", JqGridFilterRuleOp.eq,
				relationTypeId);

		return this.getService().findAllDetailByRequest(request).toPage();

	}
}
